<?php
/**
 * Created by cmb_weixin.master.
 * File: ProductOrderTransfer.php
 * User: xuewl
 * Date: 2017/12/20
 * Time: 04:16
 */

namespace app\product\service;
use app\common\library\Service;
use think\Db;
use think\Hook;

class ProductOrderTransfer extends Service
{

    use \app\common\library\traits\Model;

    protected $model;

    public function _initialize() {
        parent::_initialize();
        $this->model = model('product/product_order_transfer');
    }


    /**
     * 订单转交
     * @param $order_sn 订单号
     * @param $touid    转交给谁？
     * @param $formuid  谁转交的？
     * @return bool
     */
    public function create($order_sn, $touid, $formuid) {
        $model_order = model('product/product_order', 'service');

        $order = $model_order->where(['sn' => $order_sn, 'status' => 0])->find();

        if(!$order) {
            $this->error = '订单不存在';
            return false;
        }

        if($order['transfer_id'] > 0) {
            $this->error = '转接中，请等待接收方确认';
            return false;
        }

        \think\Db::startTrans();
        $info = [
            'order_sn' => $order_sn,
            'touid' => $touid,
            'formuid' => $formuid
        ];
        $result = $this->save($info, []);
        if(!$result) {
            $this->error = $this->getError();
            return false;
        }

        $order->transfer_id = $result['id'];
        if(!$order->save()) {
            \think\Db::rollback();
            $this->error = '服务器繁忙';
            return false;
        }

        \think\Hook::listen("ProductOrderTransferCreateAfter", $result);
        \think\Db::commit();
        return $result;
    }


    /**
     * 接收转交
     * @param string $order_sn
     * @param integer $status
     * @param integer $user_id
     * @return boolean
     */
    public function receive($order_sn, $status, $user_id) {
        $maps = [
            'order_sn' => $order_sn,
            'touid' => $user_id
        ];
        $transfer = $this->model->where($maps)->find();
        if(!$transfer) {
            $this->error = "转交单不存在";
            return false;
        }
        if($transfer['status'] != 0) {
            $this->error = "转交单非待处理状态";
            return false;
        }

        $transfer['status'] = $status;

        \think\Db::startTrans();
        $result = $transfer->save();
        if(!$result) {
            $this->error = $this->getError();
            return false;
        }
        // 变更订单状态
        if($status === 1) {
            $order = model('product/product_order', 'service');
            $order = $order->save([
                'customer_id' => $transfer['touid'],
                'transfer_id' => 0,
            ], ['sn' => $transfer['order_sn']]);
            if(!$order) {
                \think\Db::rollback();
                $this->error = '订单更新失败';
                return false;
            }
        }

        \think\Hook::listen("ProductOrderTransferReceiveAfter", $transfer);
        \think\Db::commit();
        return $transfer;

    }

}
